//
//  JSTUserInfoHeaderView.m
//  KYKJPackDemo
//
//  Created by 01 on 17/6/27.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTUserInfoHeaderView.h"

@implementation JSTUserInfoHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _headerImageView = [[UIImageView alloc] init];
        [self addSubview:_headerImageView];
        
        _iconImageView = [[UIImageView alloc] init];
        [self addSubview:_iconImageView];
        
        _sexImageView = [[UIImageView alloc] init];
        [self addSubview:_sexImageView];
        
        _nameLabel = [[UILabel alloc] init];
        [self addSubview:_nameLabel];
        
        _birthDayBgView = [[UIView alloc] init];
        [self addSubview:_birthDayBgView];
        
        _birthDayLabel = [[M80AttributedLabel alloc] init];
        [self addSubview:_birthDayLabel];
        
        _signLabel = [[UILabel alloc] init];
        [self addSubview:_signLabel];
    }
    return self;
}

/**
 *  根据模型创建内部子控件并且计算高度返回
 *
 *  @param model  JSTUserPublicModel
 *  @param height 自身的高度
 */
- (void)createContent:(JSTUserPublicModel *)model blockHeight:(void (^)(CGFloat))height
{
    _headerImageView.frame = CGRectMake(0, -50, kScreenWidth, kCurrentScreenWidth(150));
    _headerImageView.tag = 101;
    _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headerImageView.image = [UIImage imageNamed:@"my_integral_bg"];
    
    CGFloat iconX = (kScreenWidth - kCurrentScreenWidth(50))/2;
    CGFloat iconY = CGRectGetMaxY(_headerImageView.frame) - kCurrentScreenWidth(50)/2.;
    CGFloat iconW = kCurrentScreenWidth(50);
    CGFloat iconH = iconW;
    _iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    _iconImageView.layer.masksToBounds = YES;
    _iconImageView.layer.cornerRadius = iconW/2;
    
    CGFloat sexX = CGRectGetMaxX(_iconImageView.frame) - kCurrentScreenWidth(20);
    CGFloat sexY = CGRectGetMaxY(_iconImageView.frame) - kCurrentScreenWidth(20);
    CGFloat sexW = kCurrentScreenWidth(20);
    CGFloat sexH = sexW;
    _sexImageView.frame = CGRectMake(sexX, sexY, sexW, sexH);
    if (model.sex) {
        _sexImageView.image = [UIImage imageNamed:@"sex_icon_nan"];
    } else {
        _sexImageView.image = [UIImage imageNamed:@"sex_icon_nv"];
    }
    
    CGFloat nameX = 0;
    CGFloat nameY = CGRectGetMaxY(_iconImageView.frame) + kCurrentScreenWidth(10);
    CGFloat nameW = kScreenWidth;
    CGSize nameSize = [NSString sizeWithString:model.nickName font:kSystemFont(kCurrentScreenWidth(16)) CGsize:CGSizeMake(kScreenWidth, MAXFLOAT)];
    _nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameSize.height);
    _nameLabel.text = model.nickName;
    _nameLabel.font = kSystemFont(kCurrentScreenWidth(16));
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    
    [_birthDayLabel appendImage:[UIImage imageNamed:@"icon_borthday"] maxSize:CGSizeMake(kCurrentScreenWidth(10), kCurrentScreenWidth(10)) margin:UIEdgeInsetsZero alignment:M80ImageAlignmentCenter];
    [_birthDayLabel appendText:model.birthDay];
    _birthDayLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _birthDayLabel.textColor = [UIColor colorWithHexString:@"838383"];
    CGSize birthDaySize = [_birthDayLabel sizeThatFits:CGSizeMake(kScreenWidth, MAXFLOAT)];
    CGFloat birthDayX = (kScreenWidth - birthDaySize.width)/2.;
    CGFloat birthDayY = CGRectGetMaxY(_nameLabel.frame) + kCurrentScreenWidth(15);
    _birthDayLabel.frame = (CGRect){{birthDayX, birthDayY}, birthDaySize};
    _birthDayLabel.backgroundColor = [UIColor lightGrayColor];
    
    CGFloat birthDayBgX = birthDayX - kCurrentScreenWidth(10);
    CGFloat birthDayBgY = birthDayY - kCurrentScreenWidth(.5);
    CGFloat birthDayBgW = birthDaySize.width + kCurrentScreenWidth(10)*2;
    CGFloat birthDayBgH = birthDaySize.height + kCurrentScreenWidth(.5)*2;
    _birthDayBgView.frame = CGRectMake(birthDayBgX, birthDayBgY, birthDayBgW, birthDayBgH);
    _birthDayBgView.backgroundColor = [UIColor lightGrayColor];
    _birthDayBgView.layer.cornerRadius = kCurrentScreenWidth(7.5);
    
    NSString *signStr = [NSString string];
    if (IsStrEmpty(model.signature)) {
        signStr = [NSString stringWithFormat:@"简介:这家伙有点懒，什么也没留下~~"];
    } else {
        signStr = [NSString stringWithFormat:@"简介:%@", model.signature];
    }
    CGFloat signX = kCurrentScreenWidth(50);
    CGFloat signY = CGRectGetMaxY(_birthDayBgView.frame) + kCurrentScreenWidth(10);
    CGSize signSize = [NSString sizeWithString:signStr font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kScreenWidth - kCurrentScreenWidth(50*2), MAXFLOAT)];
    _signLabel.frame = (CGRect){{signX, signY}, signSize};
    _signLabel.text = signStr;
    _signLabel.textColor = [UIColor blackColor];
    _signLabel.font = kSystemFont(kCurrentScreenWidth(12));
    
    height(CGRectGetMaxY(_signLabel.frame));
}
@end
